//
//  SDuanziViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SDuanziViewController.h"
// 导入头文件
#import "SHDetailsViewController.h"
#import "SHDetails2ViewController.h"
#import "SDuanziTableViewCell.h"
#import "SDuanzi2TableViewCell.h"
#import "UIImageView+WebCache.h"
#import "SNetWorkHandle.h"
#import "SHppyObject.h"
#import "MJRefresh.h"

@interface SDuanziViewController ()<UITableViewDataSource, UITableViewDelegate>
// 属性
@property(nonatomic, retain)UITableView *table;
@property(nonatomic, retain)NSMutableData *data;
@property(nonatomic, retain)NSArray *dataArr;
@property(nonatomic, retain)NSMutableArray *ArrOne;
@property(nonatomic, retain)NSMutableArray *ArrTwo;
@property(nonatomic, retain)SHppyObject *sff;


@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@property(nonatomic ,retain)UIImageView *imageView;

@end

@implementation SDuanziViewController
- (void)dealloc
{
    [_dataArr release];
    [_data release];
    [_table release];

    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.ArrOne = [NSMutableArray array];
    self.ArrTwo = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.title =@"开心一刻";
    self.navigationController.navigationBar.translucent  =NO;
    
    self.view.backgroundColor  =[UIColor  whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(fanHui)];
    
    [self create];
    
    //请求出现小菊花
    self.imageView  = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imageView.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
    _imageView.alpha =0.6;
    [self.view addSubview:_imageView];
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:_imageView.center];
    _activityIndicator.backgroundColor= [UIColor grayColor];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [_imageView addSubview:_activityIndicator];
    [_activityIndicator startAnimating];

    [self addHeader];
    [self addFooter];
    
}
// 上拉
-(void)addHeader
{
    __block  SDuanziViewController *happy = self;
    [self.table addHeaderWithCallback:^{
        happy.isUpLoading = NO;
        [happy details];
    }];
    [self.table headerBeginRefreshing];
}

// 下拉
-(void)addFooter
{
    __block SDuanziViewController *happy1 = self;
    [self.table addFooterWithCallback:^{
        happy1.isUpLoading = YES;
        [happy1 details];
    }];
}

-(void)fanHui
{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

// 数据
-(void)details
{
    [SNetWorkHandle getDataWithURL:@"http://c.3g.163.com/recommend/getChanRecomNews?channel=duanzi&passport=&devId=AFBF8E4D-DAF7-4899-9657-5FE0F6825D11&size=20" completion:^(id result) {
        
        NSMutableDictionary *dic = result;
        _ArrOne = [dic objectForKey:@"段子"];
        
        // 重新加载数据
        if (self.isUpLoading == NO) {
            [self.ArrTwo removeAllObjects];
        }
               for (NSDictionary *dic in _ArrOne) {

            self.sff = [[SHppyObject alloc] initWithDic:dic];
            [_ArrTwo addObject:_sff];
        }
        
      // 结束上拉 下拉
        [self.table headerEndRefreshing];
        [self.table footerEndRefreshing];
        [_table reloadData];
        [_imageView removeFromSuperview];
    }];
}
//创建视图
-(void)create
{
   self.table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
  
    [self.view addSubview:_table];
    [_table release];
    
        [_table registerClass:[SDuanzi2TableViewCell class] forCellReuseIdentifier:@"c"];
        [_table registerClass:[SDuanziTableViewCell class] forCellReuseIdentifier:@"a"];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _ArrTwo.count;
    
}

// 重用池
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 判断不带图片执行方法
    if ([_ArrTwo[indexPath.row] imgsrc].length == 0 ) {
        
         SDuanzi2TableViewCell * cell = [[[SDuanzi2TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:@"c"] autorelease];
         cell.sHappy = _ArrTwo[indexPath.row];
        return cell;
        
        }
    // 带图片
        else if ([_ArrTwo[indexPath.row] imgsrc].length != 0)
    {
          SDuanziTableViewCell*  cell = [[[SDuanziTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:@"a"] autorelease];
        
        cell.Shappy = _ArrTwo[indexPath.row];
        return cell;
   
    }
    
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat f = [SDuanziTableViewCell heightWithText:[_ArrTwo[indexPath.row]digest]];
    
    if ([_ArrTwo[indexPath.row] imgsrc] == 0) {
        
        return f + 90;
    }else
        if ([_ArrTwo[indexPath.row] imgsrc] != 0 ) {
            
            return f + [UIScreen mainScreen].bounds.size.height / 2.2;
        }return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 不带图的跳转到文字详情
    if ([_ArrTwo[indexPath.row] imgsrc] ==0 ) {
        
        SHDetails2ViewController *happy1 = [[SHDetails2ViewController alloc] init];
        happy1.strOne = [_ArrTwo[indexPath.row] docid];
              
        [self.navigationController pushViewController:happy1 animated:YES];
            }
    // 带图的跳转带图片的详情
    else if([_ArrTwo[indexPath.row] imgsrc] != 0)
    {
        SHDetailsViewController *happy2 = [[SHDetailsViewController alloc] init];
        happy2.strTwo = [_ArrTwo[indexPath.row] docid];
        [self.navigationController pushViewController:happy2 animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
